//
//  loginView.m
//  YiXiu
//
//  Created by ZHY on 16/6/28.
//  Copyright © 2016年 zzz. All rights reserved.
//

#import "loginView.h"
#import "UMSocial.h"
#import "UIImageView+WebCache.h"
#import "loginModel.h"
#import "UIAlertController+Category.h"

#define   viewW  KScreenWidth - 38
#define   viewH  KScreenHeight - 20-64-35-44
#define   viewX (KScreenWidth - (KScreenWidth - 38))/2
#define   viewY 84

@interface loginView ()<UMSocialUIDelegate>

@property (nonatomic, strong) NikeBlock block;
@property (nonatomic, strong) loginView *loginV;
@property (nonatomic, strong) UIImageView * informationView;
@property (nonatomic, strong) BaseView * LoginVC;
@property (nonatomic, strong)  UITextField * nameField;
@property (nonatomic, strong)  UIButton * iconBtn;
@property (nonatomic,strong)UIButton * manBtn;
@property(nonatomic,strong)UIButton * womanBtn;
@property(nonatomic,strong)UIButton * sureBtn;







@end

@implementation loginView

-(instancetype)initWithFrame:(CGRect)frame
{
    
    if ( self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
    
        
        
//        self.backgroundColor = [UIColor whiteColor];
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        
        //点击两次
        tap.numberOfTapsRequired = 2;
        
        [self addGestureRecognizer:tap];
        
        [self addSubview:self.informationView];
        [self addSubview:self.LoginVC];
        
    }
    return self;
}

-(void)tapAction:(UITapGestureRecognizer *)tap
{
    [BaseView animateWithDuration:1 animations:^{
        
        
        self.frame = CGRectMake(0, -KScreenHeight, KScreenWidth, KScreenHeight);
        
    }];
}

- (void)orgSubViews{
//    _LoginVC.frame = CGRectMake(viewX, viewY, viewW, viewH);
    
    
}

//登录界面
-(BaseView *)LoginVC
{
    
    if (!_LoginVC) {
            BaseView * loginView =[[BaseView alloc]initWithFrame:CGRectMake(viewX, viewY,viewW ,viewH)];
            loginView.backgroundColor = [UIColor whiteColor];
            loginView.alpha = 1;
            UIImage * image = [UIImage imageNamed:@"bgbgbg"];
        loginView.backgroundColor  = [UIColor colorWithPatternImage:image];
            _LoginVC = loginView;
            [self addSubview:loginView];

            //设置登录label
            UILabel * Loginlabel = [[UILabel alloc]initWithFrame:CGRectMake((viewW-100)/2, 40, viewW, 50)];
            Loginlabel.text = @"第三方登录";
            Loginlabel.font = [UIFont boldSystemFontOfSize:25];
            [loginView addSubview:Loginlabel];
        
            //button   CGFloat btnW = self.frame.size.width -36*2;
            CGFloat btnW = viewW-36*2;
            CGFloat btnH = 107/2;
            CGFloat btnX = 36;
            CGFloat btnY = 136;
        
            //设置QQ登录按钮
            UIButton * QQLogin = [[UIButton alloc]initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
            QQLogin.backgroundColor = [UIColor yellowColor];
            [QQLogin setBackgroundImage:[UIImage imageNamed:@"qqdl"] forState:UIControlStateNormal];
            [QQLogin addTarget:self action:@selector(libLoginAction:) forControlEvents:UIControlEventTouchUpInside];
            QQLogin.layer.cornerRadius = 25;
            [loginView addSubview:QQLogin];
        

        
            //设置微信登录按钮
            UIButton * weChatLogin = [[UIButton alloc]initWithFrame:CGRectMake(btnX, btnY+40+btnH, btnW, btnH)];
            weChatLogin.layer.cornerRadius = 25;
            [weChatLogin setBackgroundImage:[UIImage imageNamed:@"wxdl"] forState:UIControlStateNormal];
            [weChatLogin addTarget:self action:@selector(weChatAction:) forControlEvents:UIControlEventTouchUpInside];
            [loginView addSubview:weChatLogin];
        

        
            //设置微博登录按钮
            UIButton * weiboLogin = [[UIButton alloc]initWithFrame:CGRectMake(btnX, btnY+80+btnH*2, btnW, btnH)];
            weiboLogin.layer.cornerRadius = 25;
            [weiboLogin setBackgroundImage:[UIImage imageNamed:@"wbdl"] forState:UIControlStateNormal];
        
            [weiboLogin addTarget:self action:@selector(sinaAction:) forControlEvents:UIControlEventTouchUpInside];
            [loginView addSubview:weiboLogin];
        
        

        
        }
    return _LoginVC;
}

-(void)libLoginAction:(UIButton *)sender
{
     UMSocialSnsPlatform *snsPlatform1 = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];

    [self animation];
    
    //此处调用授权的方法,你可以把下面的platformName 替换成 UMShareToSina,UMShareToTencent等
    NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:snsPlatform1.shareToType];
    
    [UMSocialControllerService defaultControllerService].socialUIDelegate = self;
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:platformName];
//    __block typeof(self) weakSelf = self;
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
//           获取微博用户名、uid、token、第三方的原始用户信息thirdPlatformUserProfile等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            
//            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName];
            NSString * urlStr = snsAccount.iconURL;
            
            
            [self.iconBtn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]] forState:UIControlStateNormal];
//            NSDictionary * thirdPlatformResponse = response.thirdPlatformResponse;
            
            
            
            [self senLoginName:snsAccount.userName sex:@"男" label:snsAccount.accessToken headurl:snsAccount.iconURL userid:snsAccount.usid];
        
            
//            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            
//stringByAppendingString:string2
            
            
        }
    });
}

-(void)weChatAction:(UIButton *)sender{
    
    
//    [UIAlertController showAlertViewWithTitle:@"暂时请用QQ进行登录" message:nil isCancle:@"残忍拒绝" sureTitle:@"去QQ登录" suerAction:<#^(UIAlertAction *action)suerBlock#> otherTitles:<#(NSArray *)#> otherActions:<#^(UIAlertAction *action)otherBlock, ...#>, nil]
    
    [UIAlertController showAlertViewWithTitle:@"暂时请用QQ进行登录" message:nil cancle:@"残忍拒绝" sureTitle:@"去QQ登录" sureAction:^(UIAlertAction *action) {
         [sender addTarget:self action:@selector(libLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    }];
    
}





-(void)sinaAction:(UIButton *)sender{
    UMSocialSnsPlatform *snsPlatform1 = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    [self animation];
    
    //此处调用授权的方法,你可以把下面的platformName 替换成 UMShareToSina,UMShareToTencent等
    NSString *platformName = [UMSocialSnsPlatformManager getSnsPlatformString:snsPlatform1.shareToType];
    
    [UMSocialControllerService defaultControllerService].socialUIDelegate = self;
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:platformName];
//    __block typeof(self) weakSelf = self;
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//           获取微博用户名、uid、token、第三方的原始用户信息thirdPlatformUserProfile等
        if (response.responseCode == UMSResponseCodeSuccess) {
//            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:platformName];
            NSString * urlStr = snsAccount.iconURL;
            
            [self.iconBtn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]]] forState:UIControlStateNormal];
//            NSDictionary * thirdPlatformResponse = response.thirdPlatformResponse;

        }
        
    });
}



- (void)senLoginName:(NSString *)name sex:(NSString *)sex label:(NSString *)label headurl:(NSString *)url userid:(NSString *)userid{
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"u_name"] = name;
    dic[@"u_sex"]= sex;
    dic[@"u_label"] = label;
    dic[@"u_headUrl"] = url;
    dic[@"userid"] = userid;
    
    [[UserModel sharedUserModel] setUserID:userid];
    
    [NetWorkingTool POST:@"users/addAndLogin" parameters:dic success:^(id responseObject, NSError *error) {
        
        NSLog(@"------zzz----%@",[responseObject mj_JSONObject]);
        
        loginModel * model = [loginModel mj_objectWithKeyValues:responseObject];

        
        [[UserModel sharedUserModel] setIsLogin:YES];
        [[UserModel sharedUserModel] setHeadImage:model.u_headUrl];
        [[UserModel sharedUserModel] setSex:model.u_sex];
        [[UserModel sharedUserModel] setU_id:model.u_id];
        [[UserModel sharedUserModel] setName:model.u_name];
       

        BOOL is = [NSKeyedArchiver archiveRootObject:[UserModel sharedUserModel] toFile:UserPath];
        YXLog(@"%d",is);
        
    } failure:^(id responseObject, NSError *error) {
        [[UserModel sharedUserModel] setIsLogin:NO];

        [NSKeyedArchiver archiveRootObject:[UserModel sharedUserModel] toFile:UserPath];

        NSLog(@"-----%@----",error);
    }];
    

}

//基本信息界面
-(UIImageView *)informationView
{
    if (!_informationView) {


        _informationView =[[UIImageView alloc]initWithFrame:CGRectMake(viewX, viewY,viewW ,viewH)];
//        informationView.backgroundColor = [UIColor whiteColor];
        
        [_informationView setImage:[UIImage imageNamed:@"bgbgbg"]];
        [self addSubview:_informationView];
        
        //设置登录Image
        CGFloat headProW = 50;
        CGFloat headProH = 50;
        CGFloat headProY = 40;
        CGFloat headProX = (viewW -headProW)/2;
        UIButton * headPro = [[UIButton alloc]initWithFrame:CGRectMake(headProX, headProY, headProW, headProH)];
        headPro.backgroundColor = [UIColor purpleColor];
        self.iconBtn = headPro;
       
        [_informationView addSubview:headPro];
        
        //设置昵称label
        CGFloat nameX = 20;
        CGFloat nameY =headProY+headProH+30;
        CGFloat nameW =60;
        CGFloat nameH =30;
        UILabel * nikeName = [[UILabel alloc]initWithFrame:CGRectMake(nameX, nameY, nameW, nameH)];
        nikeName.text = @"昵称";
        nikeName.font = [UIFont boldSystemFontOfSize:18];
        [_informationView addSubview:nikeName];
        
        //设置输入昵称的输入框
        CGFloat fieldX = nameX+nameW+10;
        CGFloat fieldY = nameY;
        CGFloat fieldW = viewW-fieldX-25;
        CGFloat fieldH = nameH;
        if (!_nameField) {
            _nameField = [[UITextField alloc]initWithFrame:CGRectMake(fieldX, fieldY, fieldW, fieldH)];
            _nameField.placeholder = @"请输入您的昵称";
            [_informationView addSubview:_nameField];
        }
        
        //设置性别label
        CGFloat sexLabelX = nameX;
        CGFloat sexLabelY = nameY+nameH+15;
        CGFloat sexLabelW = nameW;
        CGFloat sexLabelH = nameH;
        
        UILabel * sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(sexLabelX, sexLabelY, sexLabelW, sexLabelH)];
        sexLabel.text = @"性别";
        sexLabel.font = [UIFont boldSystemFontOfSize:18];
        [_informationView addSubview:sexLabel];
        
        //设置男女的button
        CGFloat manX = fieldX;
        CGFloat manY = sexLabelY;
        CGFloat manW = fieldW/2 - 10;
        CGFloat manH = fieldH;
        
        UIButton * manButton = [[UIButton alloc]initWithFrame:CGRectMake(manX, manY, manW, manH)];
    
        [manButton setImage:[UIImage imageNamed:@"nan"] forState:UIControlStateNormal];
        
        [_informationView addSubview:manButton];
        
        //女
        CGFloat womanX = fieldX+manW+10;
        UIButton * womanButton = [[UIButton alloc]initWithFrame:CGRectMake(womanX, manY, manW, manH)];
        [womanButton setImage:[UIImage imageNamed:@"nv"] forState:UIControlStateNormal];
        [_informationView addSubview:womanButton];
//        womanButton.backgroundColor = [UIColor yellowColor];
        
//        UIImageView * womanImage = [[UIImageView alloc]initWithFrame:CGRectMake(leftImageX, leftImageY, leftImageW, leftImageH)];
//        womanImage.backgroundColor = [UIColor blackColor];
//        [womanButton addSubview:womanImage];
//        
//        UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(rightLabelX, rightLabelY, rightLabelW, rightLabelH)];
//        womanLabel.text = @"女";
//        [womanButton addSubview:womanLabel];
        
        
        CGFloat nextW = 94;
        CGFloat nextH = 30;
        CGFloat nextX = (KScreenWidth - nextW)/2;
        CGFloat nextY = viewH - nextH - 60;
        
        
//        UIButton * nextButton = [[UIButton alloc]initWithFrame:CGRectMake(nextX, nextY, nextW, nextH)];
//        [nextButton setTitle:@"下一步" forState:UIControlStateNormal];
//        [nextButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [nextButton addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
//        [_informationView addSubview:nextButton];
        
        
        UIButton * nextButton = [[UIButton alloc]initWithFrame:CGRectMake(nextX, nextY, nextW, nextH)];
//        [backButton setTitle:@"确认" forState:UIControlStateNormal];
        [nextButton setImage:[UIImage imageNamed:@"wc"] forState:UIControlStateNormal];
//        [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [nextButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
        [_informationView addSubview:nextButton];
    }

    return _informationView;
}

-(void)backButton:(UIButton *)sender{

    [self animation];

   
}

- (void)animation{
    
    CGContextRef context=UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
    
    [self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    
    self.nameField.text = nil;
    
}

-(void)addNickButton:(NikeBlock)block
{
    self.block = block;
}

-(void)nextButton:(UIButton *)sender
{

    [UIView animateWithDuration:1 animations:^{
        self.frame = CGRectMake(0, -KScreenHeight, KScreenWidth, KScreenHeight);
    } completion:^(BOOL finished) {
        
        
        [self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
        
    }];
    
    
    [[UserModel sharedUserModel] setName:self.nameField.text];
}


@end
